package com.regex.web.vo;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import lombok.Data;

@Data
public class ResultVO<T> implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 4560633801107550600L;
	/**
	 * 0 成功 其他失败
	 */
	private int code;
	/**
	 * 接口返回说明信息
	 */
	private String msg;
	/**
	 * 数据结构体
	 */
	private T data;
	
	public static int SUCCESS = 1;
	public static int ERROR = 2;
	public static Map<Integer, String> errors = new HashMap<Integer, String>();
	static {
		errors.put(SUCCESS, "成功");
		errors.put(ERROR, "失败");
	}
	
	public ResultVO() {}
	public ResultVO(int code, String msg) {
		this.code = code;
		this.msg = msg;
	}
	
	/**
	 * 失败
	 * @param <T>
	 * @param data
	 * @param code
	 * @param msg
	 * @return
	 */
	public static <T> ResultVO<T> error(T data, int code, String msg){
		ResultVO<T> result = new ResultVO<T>(code, msg);
		result.setData(data);
		return result;
	}
	
	/**
	 * 失败
	 * @param <T>
	 * @param code
	 * @param msg
	 * @return
	 */
	public static <T> ResultVO<T> error(String msg){
		ResultVO<T> result = new ResultVO<T>(ResultVO.ERROR, msg);
		return result;
	}
	
	/**
	 * 失败
	 * @param <T>
	 * @param code
	 * @param msg
	 * @return
	 */
	public static <T> ResultVO<T> error(int code, String msg){
		ResultVO<T> result = new ResultVO<T>(code, msg);
		return result;
	}
	
	/**
	 * 成功
	 * @param <T>
	 * @param data
	 * @param code
	 * @param msg
	 * @return
	 */
	public static <T> ResultVO<T> success(T data, int code, String msg){
		ResultVO<T> result = new ResultVO<T>(code, msg);
		result.setData(data);
		return result;
	}
	
	/**
	 * 成功
	 * @param <T>
	 * @param code
	 * @param msg
	 * @return
	 */
	public static <T> ResultVO<T> success(int code, String msg){
		ResultVO<T> result = new ResultVO<T>(code, msg);
		return result;
	}
	/**
	 * 成功
	 * @param <T>
	 * @param msg
	 * @return
	 */
	public static <T> ResultVO<T> success(String msg){
		ResultVO<T> result = new ResultVO<T>(ResultVO.SUCCESS, msg);
		return result;
	}
	
	/**
	 * 成功
	 * @param <T>
	 * @return
	 */
	public static <T> ResultVO<T> success(){
		ResultVO<T> result = new ResultVO<T>(ResultVO.SUCCESS, ResultVO.errors.get(ResultVO.SUCCESS));
		return result;
	}

}
